$(function () {

    //显示角色职位
    $("#user_role").append("<option value=''>"+"请选择"+"</option>")
    $.ajax({
        url: "../../DropDownBoxQuery/findRole",
        type: "post",
        dataType: "json",
        success: function (map) {
            var roleList = map.roleList;
            var department_select = document.getElementById("user_role");
            //加载
            for (var i = 0; i < roleList.length; i++) {
                var opt = document.createElement("option");
                opt.innerText = roleList[i].roleName;
                opt.setAttribute("value", roleList[i].roleId);
                department_select.appendChild(opt);
            }
        },
        error: function () {
            console.log("数据返回失败");
        }
    });
    //显示科室
    $("#id_select").append("<option value=''>"+"请选择"+"</option>")
    $.ajax({
        url: "/getkslist",
        type: "post",
        datatype: "json",
        success: function (result) {
            for (var i = 0; i < result.departmentlist.length; i++) {

                $("#id_select").append("<option value='"+result.departmentlist[i].departmentId +"'>"

                    + result.departmentlist[i].departmentName + "</option>");
            }

        }
    });
    /**
     * t添加事件
     */
    $("#add").click(function () {
        $("#div2").hide();
        $("#div3").show();
        $("#div5").hide();
        $("#div4").show();
        alert("请将以上信息全部添加选择")
        $("#bc").click(function () {
            $("#userform").attr("action","/addMeaningUser")
            $("#userform").submit();
        })
        $("#fh").click(function () {
            $("#div2").show();
            $("#div3").hide();
            $("#div5").show();
            $("#div4").hide();
            $('#password').prop("disabled", false);//允许输入；
            $(":input", "#userform")
                .not(":button", ":reset", "hidden", "submit")
                .val("")
                .removeAttr("checked")
                .removeAttr("selected");
        })
    })

//复选框全选按钮操作
    $("#all").click(function () {
        $("input[type='checkbox']").each(function () {
            if ($(this).prop("checked") == true) {
                $("input[type='checkbox']").prop('checked', true);
                return;
            } else {
                $("input[type='checkbox']").prop('checked', false);
                return;
            }
        });
    })
//删除被选中的数据
    $("#del").click(function () {
        var id_array = new Array();
        var idstr = "";
        $("input[name='check']:checked").each(function () { // 遍历选中的checkbox
            var pidSel = $(this).val();
            console.log(pidSel);
            id_array.push(pidSel);
            idstr = id_array.join(',');
        });
        console.log(id_array);
        for (var i = 0; i < id_array.length; i++) {
            var userid = id_array[i];
            console.log(userid);
            $.ajax({//ajax 调用删除方法接口。。。。})
                url: "/deladdMeaningUser",
                data: {
                    id: userid
                },
                datatype: "json",
                type: "post",
                success: function (result) {
                        location.href = "/selectuser";
                }
            })
        }
            alert("删除成功");
    })

    //通过id回显用户信息
    $("#xg").click(function () {
        $("#div2").hide();
        $("#div3").show();
        $("#div5").hide();
        $("#div4").show();
        $('#password').prop("disabled", true);//禁止输入；
        var id_array = new Array();
        var idstr = "";
        $("input[name='check']:checked").each(function () { // 遍历选中的checkbox
            var pidSel = $(this).val();
            console.log(pidSel);
            id_array.push(pidSel);
            idstr = id_array.join(',');
        });
        console.log(id_array);

        for (var i = 0; i < id_array.length; i++) {
            var userid = id_array[i];
            console.log(userid);
            $.ajax({//ajax 调用删除方法接口。。。。})
                url: "/selectuserbyid",
                data: {
                    id: userid
                },
                datatype: "json",
                type: "post",
                success: function (result) {
                    console.log(result);
                    $("#name").val(result.User.username);
                    $("#phone").val(result.User.userPhone);
                    $("#status").val(result.User.userSex);
                    $("#email").val(result.User.userEmail);
                    $("#id_select").append(
                        '<option value='
                        + result.User.departmentId + ' selected>'
                        + result.User.departmentName + '</option>');
                    $("#user_role").append(
                        '<option value='
                        + result.User.roleId + ' selected>'
                        + result.User.roleName + '</option>');
                    $("#statu").val(result.User.userStatus);

                }
            });
            $("#bc").click(function () {
                var data = $.param({userId: userid}) + "&" + $("#userform").serialize() + "";
                $.ajax({
                    url: "/upMeaningUser",
                    data: data,
                    datatype: "json",
                    type: "post",
                    success: function (result) {
                        console.log(result);
                        location.href = "/selectuser";
                    }
                })
            })
        }
        $("#fh").click(function () {
            $("#div2").show();
            $("#div3").hide();
            $("#div5").show();
            $("#div4").hide();
            $('#password').prop("disabled", false);//允许输入；
            $(":input", "#userform")
                .not(":button", ":reset", "hidden", "submit")
                .val("")
                .removeAttr("checked")
                .removeAttr("selected");
        })
    });
    $("#js").click(function () {
        $("#userform").attr("action", "/selectuser");
        $("#userform").submit();
    })
    //清空
    $("#qk").click(function () {

        $(":input", "#userform")
            .not(":button", ":reset", "hidden", "submit")
            .val("")
            .removeAttr("checked")
            .removeAttr("selected");
    })

})